/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taikai;

import java.awt.Color;
import java.awt.Component;
import java.awt.SystemColor;
import java.io.Serializable;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author jeffmartin
 */
public class PersonRenderer extends DefaultTableCellRenderer implements Serializable {

    private PeopleData people;

    @Override
    protected void setValue(Object o) {
        if (o instanceof Grade) {
            setText(((Grade) o).title);
        } else if (o instanceof Team) {
            setText(((Team) o).getName());
        } else {
            super.setValue(o);
        }
    }

    @Override
    public Component getTableCellRendererComponent(JTable jtable, Object o, boolean isSelected, boolean hasFocus, int row, int col) {
        final Person person = getPeople().getPerson(row);

        final Component component = super.getTableCellRendererComponent(jtable, o, isSelected, hasFocus, row, col);
        setBackgroundColour(isSelected, hasFocus, person, component);

        return component;
    }

    private void setBackgroundColour(boolean isSelected, boolean hasFocus, final Person person, Component component) {
        Color background = SystemColor.text;
        Color foreground = SystemColor.textInactiveText;

        if (person.getTeam() != null) {
            foreground = SystemColor.textText;
        }

        if (isSelected) {
            background = SystemColor.controlHighlight;
            foreground = SystemColor.controlLtHighlight;
        }

        component.setBackground(background);
        component.setForeground(foreground);

    }

    /**
     * @return the people
     */
    public PeopleData getPeople() {
        return people;
    }

    /**
     * @param people the people to set
     */
    public void setPeople(PeopleData people) {
        this.people = people;
    }
}
